package com.hyfd.service.mp;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.hyfd.common.BaseJson;
import com.hyfd.common.Page;
import com.hyfd.common.utils.DateTimeUtils;
import com.hyfd.dao.mp.AgentDao;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.service.BaseService;

import net.sf.jxls.transformer.XLSTransformer;

@Service
public class ProviderReportSer extends BaseService{

	@Autowired
	OrderDao orderDao;
	@Autowired
	AgentDao agentDao;
	
	/**
	 * 流量通道结算账单
	 * @author lks 2017年5月22日上午11:19:54
	 * @param request
	 * @param response
	 * @return
	 */
	public String ProviderFlowStatementList(HttpServletRequest request,HttpServletResponse response){
		Map<String,Object> param = getMaps(request);
		param.put("bizType", "1");
		//Map<String,Object> user = getUser();
		StringBuilder sb=new StringBuilder();
		try{
			Page p=getPage(param);//提取分页参数
			int total = orderDao.countProviderStatementList(param);
			p.setCount(total);
			int pageNum=p.getCurrentPage();
			int pageSize=p.getPageSize();
			
			sb.append("{");
			sb.append(""+getKey("page")+":"+p.getCurrentPage()+",");
			sb.append(""+getKey("total")+":"+p.getNumCount()+",");
			sb.append(""+getKey("records")+":"+p.getCount()+",");
			sb.append(""+getKey("rows")+":"+"");
			
			PageHelper.startPage(pageNum, pageSize);//mybatis分页插件
			List<Map<String, Object>> dataList = orderDao.selectProviderStatementList(param);
			String dataListJson=BaseJson.listToJson(dataList);
			sb.append(dataListJson);
			sb.append("}");
		}catch(Exception e){
			log.error("订单提交记录列表查询方法出错"+e.getMessage());
		}
		
		return sb.toString();
	}
	
	/**
	 * 订单详情跳转方法
	 * @author lks 2017年5月31日下午4:03:38
	 * @param request
	 * @param response
	 * @return
	 */
	public String toDetailFlowOrderList(HttpServletRequest request,HttpServletResponse response){
		Map<String,Object> param = getMaps(request);
		Session session=getSession();
		session.setAttribute("param", param);
		return "mp/detailFlowOrderList";
	}
	
	/**
	 * 详情订单
	 * @author lks 2017年5月31日下午5:15:49
	 * @param request
	 * @param response
	 * @return
	 */
	public String detailFlowOrderList(HttpServletRequest request,HttpServletResponse response){
		Map<String,Object> param = getMaps(request);
		StringBuilder sb=new StringBuilder();
		try{
			Page p=getPage(param);//提取分页参数
			int total = orderDao.countDetailOrderList(param);
			p.setCount(total);
			int pageNum=p.getCurrentPage();
			int pageSize=p.getPageSize();
			
			sb.append("{");
			sb.append(""+getKey("page")+":"+p.getCurrentPage()+",");
			sb.append(""+getKey("total")+":"+p.getNumCount()+",");
			sb.append(""+getKey("records")+":"+p.getCount()+",");
			sb.append(""+getKey("rows")+":"+"");
			
			PageHelper.startPage(pageNum, pageSize);//mybatis分页插件
			List<Map<String, Object>> dataList = orderDao.selectDetailOrderList(param);
			String dataListJson=BaseJson.listToJson(dataList);
			sb.append(dataListJson);
			sb.append("}");
		}catch(Exception e){
			getMyLog(e,log);
		}
		
		return sb.toString();
	}
	
	/**
	 * 导出数据库数据
	 * @author lks 2017年5月26日下午2:51:25
	 * @param request
	 * @param resposne
	 */
	public void exportFlowOrderExcel(HttpServletRequest request,HttpServletResponse response){
		Map<String,Object> param = getMaps(request);
		String agentId = (String) param.get("agentId");
		String fileName = DateTimeUtils.formatDate(new Date(), "yyyy/MM/dd_hh:mm:ss");
		if(agentId != null && !agentId.equals("")){
			String agentName = (String) agentDao.selectById(agentId).get("name");
			fileName = agentName + "-" + fileName;
		}
		List<Map<String,Object>> list = orderDao.selectDetailOrderList(param);
		String templatePath = request.getServletContext().getRealPath("/")+File.separator + "downloadFiles"+ File.separator+"reportTemplate.xlsx";
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("orderList", list);
		XLSTransformer former = new XLSTransformer();
		try{
			response.setContentType("application/x-excel;charset=utf-8");
	        response.setHeader("Content-Disposition", "attachment;filename=Report-"+fileName+".xlsx");
	        response.setCharacterEncoding("utf-8");
	        FileInputStream in = new FileInputStream(templatePath);
	        XSSFWorkbook workbook = (XSSFWorkbook) (former.transformXLS(in, map));
	        OutputStream os = response.getOutputStream();
	        workbook.write(os);
	        os.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
}
